

//$('<textarea id="paths"></textarea><br><button id="addPaths" class="button button-primary" "="">添加</button>').insertBefore($("#addtag"));  
var cats = [];
var pathnum = 0;
var paths
var postkey
var row = 1;
var okpaths = [];
var okpath = [];
$(function () {
    postkey = $("#_wpnonce_add-tag").val();
    $("#addPaths").click(function () {//点击按钮出发事件
        getPath();
    })

});


function addCatByPaths() {
    okpath = [];//初始化
    addcats(paths[pathnum])
}


function getPath() {
    pathnum = 0;
    row = 1
    var pathstr = $("#paths").val();
    if (pathstr.trim() == "") {
        alert("内容为空");
        return [];
    } else {
        paths = pathstr.split(/[\n]/);
        paths = trimSpace(paths);
        //得到所有行，可以先去掉不必要的行，例如A/B和A/B/C就可以不要前者	
        paths = pathsRremove(paths);//好像有BUG，多执行几次来解决吧0.0
        paths = pathsRremove(paths);//好像有BUG，多执行几次来解决吧0.0
        paths = pathsRremove(paths);//好像有BUG，多执行几次来解决吧0.0
        addCatByPaths();
    }
}

function pathsRremove(array) {

    for (var i = 0; i < array.length; i++) {
        for (var j = i + 1; j < array.length; j++) {
            var q = array[i].trim();
            var h = array[j].trim();

            if (h.indexOf(q) >= 0)  //这里以后可以修改=0时，必须开头都是一样的，这样时中间存在一样的就删掉
            {
                array.splice(i, 1);
                //i= i-1;  

            }
        }
    }
    return array;

}


function addcats(path) {

    var path1 = path;
    cats = [];//重置	
    cats = path1.split("\/");
    cats = trimSpace(cats);
    if (cats.length > 0) {
        postcat(cats[0]);
    }
}


function postcat2(name, pid) {
    $.post("edit-tags.php", {
        action: "add-tag",
        screen: "edit-category",
        taxonomy: "category",
        post_type: "post",
        "_wpnonce_add-tag": postkey,
        //"_wp_http_referer":"/wp-admin/edit-tags.php?taxonomy=category",
        "tag-name": name,
        parent: pid[0]             //为空则为一级，否则填写父id
    },
        function (data) {
            var regex1 = /(显示选项).+?search-form/g;
            var regex2 = /(CACIE_Items).+?}};/g;
            var regex3 = /(<p>项目).+?(添加。)/g;
            var str = data;
            str = str.replace("\r|\n|\\s", "");
            //str = str.replace(/<\/?.+?>/g,"");
            str = str.replace(/[\r\n]/g, "");
            //	str = str.replace(/^\s+|\s+$/g, '')//替换空格
            str = str.replace(/\\t|\r|\n/g, '')//替换空格
            str = str.replace(/\s|\r|\n|\t/g, '')//替换空格
            var result1 = str.match(regex1);;
            var result2 = str.match(regex2);
            var result3 = str.match(regex3);
            var CACIE_Items_Str = result2[0];


            for (var i = 0; i < result3.length; i++) {
                console.log(result3[i] + "[" + name + "]");
            }
            addNextCat(CACIE_Items_Str, name);
        });
}
function postcat(name) {
    $.post("edit-tags.php", {
        action: "add-tag",
        screen: "edit-category",
        taxonomy: "category",
        post_type: "post",
        "_wpnonce_add-tag": postkey,
        //"_wp_http_referer":"/wp-admin/edit-tags.php?taxonomy=category",
        "tag-name": name,
        parent: ""              //为空则为一级，否则填写父id
    },
        function (data) {
            var regex1 = /(显示选项).+?search-form/g;
            var regex2 = /(CACIE_Items).+?}};/g;
            var regex3 = /(<p>项目).+?(添加。)/g;
            var str = data;
            str = str.replace("\r|\n|\\s", "");
            str = str.replace(/[\r\n]/g, "");
            str = str.replace(/\\t|\r|\n/g, '')//替换空格
            str = str.replace(/\s|\r|\n|\t/g, '')//替换空格      
            var result1 = str.match(regex1);;
            var result2 = str.match(regex2);
            var result3 = str.match(regex3);
            var CACIE_Items_Str = result2[0];


            for (var i = 0; i < result3.length; i++) {
                console.log(result3[i] + "[" + name + "]");
            }

            addNextCat(CACIE_Items_Str, name);
        });
}
function getid(strp) {//通过名字得到id
    var id = []
    for (var m in CACIE_Items) {
        if (CACIE_Items[m].object.name == strp) {
            id.push(CACIE_Items[m].ID);
        }
    }
    return id;
}
var num;
function addNextCat(CACIE_Items_Str, name) {

    CACIE_Items_Str = CACIE_Items_Str.replace(/CACIE_Items=/g, '')
    CACIE_Items_Str = CACIE_Items_Str.replace(/}};/g, '}}')
    CACIE_Items = eval('(' + CACIE_Items_Str + ')');
    okpath.push(name);
    var str1 = '';

    for (var i = 0; i < okpath.length; i++) {
        str1 = str1 + okpath[i] + "/";
    }

    var okpathFlag = okpaths.indexOf(str1);
    if (okpathFlag == -1) {
        okpaths.push(str1);
    }






    num = cats.indexOf(name);
    var str2 = '';
    for (var i = 0; i <= num + 1; i++) {
        str2 = str2 + cats[i] + "/";
    }
    var haveFlag = okpaths.indexOf(str2);



    if (num >= 0) {
        if (num == cats.length - 1) {
            var ok = ""; var zong = "";
            for (var i = 0; i < pathnum + 1; i++) {
                ok = ok + ">";
            }
            for (var i = 0; i < paths.length - pathnum - 1; i++) {
                zong = zong + "*";
            }
            console.log("                                         " + ok + zong + "进度：" + (pathnum + 1) + "/" + (paths.length));
            if (pathnum == paths.length - 1) {
                console.log("                                         " + ok + zong + "所有执行完毕！");
            } else {
                pathnum = pathnum + 1;

                addCatByPaths();//执行下一行
            }
        } else {


            if (haveFlag == -1) {
                postcat2(cats[num + 1], getid(name.replace(/\s|\r|\n|\t/g, '')));
            } else {
                addNextCat(CACIE_Items_Str, cats[num + 1]);//执行下一个
            }
            //

        }

    }
}



function trimSpace(array) {
    for (var i = 0; i < array.length; i++) {

        if (array[i].trim() == "" || array[i] == null || typeof (array[i]) == "undefined") {
            array.splice(i, 1);
            i = i - 1;

        }
    }
    return array;
}
















function catDelect(arr) {

    $.post("edit-tags.php", {
        taxonomy: "category",
        post_type: "post",
        "_wpnonce": "d0a40e4f52",
        //"_wp_http_referer":"/wp-admin/edit-tags.php?taxonomy=category",
        action: "delete",

        "delete_tags": arr
    },
        function (data) {
            const regex1 = /(显示选项).+?search-form/g;
            const regex2 = /(父级).+?<\/select/g;
            const regex3 = /(<p>).+?(删除。)/g;
            str = data;
            str = str.replace("\r|\n|\\s", "");
            //str = str.replace(/<\/?.+?>/g,""); 
            str = str.replace(/[\r\n]/g, "");
            //	str = str.replace(/^\s+|\s+$/g, '')//替换空格
            str = str.replace(/\\t|\r|\n/g, '')//替换空格
            str = str.replace(/\s|\r|\n|\t/g, '')//替换空格
            console.log("error:");
            var result1 = str.match(regex1);;
            for (var i = 0; i < result1.length; i++) {
                console.log(result1[i]);
            }
            console.log("父级:");
            var result2 = str.match(regex2);;
            for (var i = 0; i < result2.length; i++) {
                console.log(result2[i]);
            }
            var result3 = str.match(regex3);;
            for (var i = 0; i < result3.length; i++) {
                console.log(result3[i]);
            }

        });


}




